package com.community.web.user.controller.parking;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.community.common.exception.NeusoftException;
import com.community.common.login.LoginUserHolder;
import com.community.common.result.Result;
import com.community.common.result.ResultCodeEnum;
import com.community.model.entity.ParkingBind;
import com.community.web.user.service.ParkingBindService;
import com.community.web.user.service.ParkingService;
import com.community.web.user.vo.ParkingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "车位模块")
@RestController
@RequestMapping("/user/parking")
public class ParkingController {
    @Autowired
    private ParkingService service;

    @Autowired
    private ParkingBindService bindService;

    @ApiOperation("根据用户id获取车位列表")
    @GetMapping()
    public Result<List<ParkingVo>> getParkingList(){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        List<ParkingVo> list = service.selectList(userId);
        return Result.ok(list);
    }

    @ApiOperation("添加车牌号")
    @PostMapping()
    public Result addCar(@RequestBody ParkingBind parkingBind){
        //先判断当前车位是否有两辆车
        LambdaQueryWrapper<ParkingBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ParkingBind::getParkingId, parkingBind.getParkingId());
        long count = bindService.count(queryWrapper);
        if (count == 2){
            throw new NeusoftException(ResultCodeEnum.PARING_ERROR);
        }
        bindService.save(parkingBind);
        return Result.ok();
    }
}
